package _func

import (
	"fmt"
	"reflect"
	"runtime"
	"math"
)

func main() {
	fmt.Println( eval1(5, 6, "*"))
	
	fmt.Println(div(13, 4))
	
	q , _ := div(15, 9) //丢弃第二个参数
	fmt.Println(q)
	
	
	fmt.Println(apply(pow, 3, 4))
	
	fmt.Println(apply(func(a int, b int) int {//匿名函数  函数式编程
		return int(math.Pow(float64(a), float64(b)))
	}, 3, 4))
	
	fmt.Println(apply(func(a int, b int) int {
		return int(math.Pow(float64(a), float64(b)))
	}, 2, 4))
	
	
	
	fmt.Println(sumArgs(1, 2, 3, 4, 5, 6, 7))
	
	fmt.Print("--------------line------------------\n")
	
	
	
	a, b := 3, 4
	//swap(&a, &b)
	
	a, b = swap1(a, b)
	fmt.Println(a, b)
}

func eval(a, b int, op string) int {
	switch op {
	case "+":
		return a + b
	case "-":
		return a - b
	case "*":
		return a * b
	case "/":
		return a / b
	default:
		panic("unsupported operation: " + op)
	}
}


func eval1(a, b int, op string) (int, error) {
	switch op {
	case "+":
		return a + b, nil
	case "-":
		return a - b, nil
	case "*":
		return a * b, nil
	case "/":
		return a / b, nil
	default:
		return 0, fmt.Errorf("unsupported operation: %s", op)
	}
}

// 13 / 4 = 3 ... 1
//func div(a, b int) (q, r int) {
func div(a, b int) (int, int) {
	return a / b, a % b
}

func div1(a, b int) (q, r int) { //另外一种写法
	q = a / b
	r = a % b
	return
}



//-------line----------

func apply(op func(int, int) int, a, b int) int {
	p := reflect.ValueOf(op).Pointer()
	opName := runtime.FuncForPC(p).Name()
	fmt.Printf("Calling function %s with args (%d + %d) \n", opName, a, b)
	return op(a, b)
}

func pow(a, b int) int {
	return int(math.Pow(float64(a), float64(b)))
}

func sumArgs(value ...int) int {
 	sum := 0
	for i := range value {
		sum += value[i]
	}
	return sum
}

func swap(a, b *int)  {
	*b, *a = *a, *b
}

func swap1(a, b int) (int, int) {
	return b, a
}